package  {
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import wibrst.utils.ByteUtil;
	import wibrst.utils.VisualUtil;
	import wibrst.visual.debug.VRegMark;
	
	/**
	 * ...
	 * @author wibrst@qq.com
	 */
	public class MZoomConstrainScale extends Sprite {
		
		public function MZoomConstrainScale() {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			trace( vTest.getBounds( this ) );
			trace( vTest.getBounds( vTest ) );
			
			trace( "c:", vTest.transform.matrix.c , "b:", vTest.transform.matrix.b ) ;
			trace( "a:", vTest.transform.matrix.a , "d:", vTest.transform.matrix.d ) ;
			//trace( "skew-x:", Math.atan( vTest.transform.matrix.c ), "skew-y:", Math.atan( vTest.transform.matrix.b ) );
			
			var bmp:Bitmap = new Bitmap( VisualUtil.getBmdCopyOfDisp( vTest ) );
			bmp.x = txImgShow.x + 15;
			bmp.y = txImgShow.y + txImgShow.height + 15;
			super.addChild( bmp );
			
			var vRegMark:VRegMark = new VRegMark;
			//super.addChild( vRegMark );
			
			//VisualUtil.reviseSkew( vTest );
			stage.addEventListener( MouseEvent.CLICK, clickHandler );
			this._nWay = -1;
			
			this.txInfo.multiline = true;
			this.txInfo.selectable = false;
			this.txInfo.embedFonts = false;
			
			this.txInfoOrig.multiline = true;
			this.txInfoOrig.selectable = false;
			this.txInfoOrig.embedFonts = false;
			var sInfoOrig:String = 
				"<p><i>"
				+ "target object matrix original:" 
				+ "<br>"
				+ vTest.transform.matrix.toString()
				+ "</i> </p>";
			this.txInfoOrig.htmlText = sInfoOrig;
			
			this.vTest.visible = false;
		}
		
		private function clickHandler(e:MouseEvent):void {
			this._nWay = ( this._nWay + 1 ) % 6;
			trace ( this, "clickHandler:", this._nWay );
			
			var vFrame:Sprite;
			var bFill:Boolean;
			
			this.vTest.visible = true;
			switch ( this._nWay ) {
			case 0: vFrame = vFrame1; bFill= true; break;
			case 1: vFrame = vFrame1; bFill= false; break;
			case 2: vFrame = vFrame2; bFill= true; break;
			case 3: vFrame = vFrame2; bFill= false; break;
			case 4: vFrame = vFrame3; bFill= true; break;
			case 5: vFrame = vFrame3; bFill= false; break;
			}
			
			var sInfo:String = 
				"<p><i>"
				+ "frame reference:" + vFrame.name 
				+ "<br>"
				+ ( bFill?"fill fit frame":"shrink fit frame" )
				+ "</i> </p>";
			//trace( sInfo );
			
			this.txInfo.htmlText = sInfo;
			VisualUtil.fitDispOnDisp( vTest, vFrame, bFill );
			
			trace( 
				 "a:", vTest.transform.matrix.a.toFixed( 2 ) , "d:", vTest.transform.matrix.d.toFixed( 2 )
				,"c:", vTest.transform.matrix.c.toFixed( 2 ) , "b:", vTest.transform.matrix.b.toFixed( 2 )
				,"tx:", vTest.transform.matrix.tx.toFixed( 2 ) , "d:", vTest.transform.matrix.ty.toFixed( 2 )
			);
		}
		
		public var vTest:Sprite;
		public var vFrame1:Sprite;
		public var vFrame2:Sprite;
		public var vFrame3:Sprite;
		
		public var txImgShow:TextField;
		public var txInfoOrig:TextField;
		public var txInfo:TextField;
		
		private var _nWay:Number;
		
	}

}